// work_class/work_class.js
const workingGroupDB = wx.cloud.database().collection("workingGroup")
const groupMemberDB = wx.cloud.database().collection("workingGroup")
const {
  $Toast
} = require('../../dist/base/index');
const app = getApp()

Page({
  data: {
    groupName: '',
    spinShow: false,
    groupId: null,
    type: '',
    deleteAll: null
  },
  onLoad: function(options) {
    let data = JSON.parse(options.data)
    this.setData({
      type: data.type
    })
    if (data.type == '1') {
      this.setData({
        groupName: data.groupName,
        groupId: data.groupId
      })
    } else if (data.groupId) {
      this.setData({
        groupId: data.groupId,
        deleteAll: data.deleteAll
      })
    }
  },
  addGroup() {
    let that = this
    if (this.data.groupName) {
      this.setData({
        spinShow: true
      })
      if (this.data.type == '0') {
        //添加新组
        workingGroupDB.add({
            data: {
              groupName: that.data.groupName,
              pid: null
            }
          })
          .then(res => {
            //把自己加入新组内
            wx.cloud.callFunction({
                name: 'addGroupMember',
                data: {
                  groupId: res._id
                }
              })
              .then(res2 => {
                if (res2.result._id) {
                  if (that.data.groupId) { //从原来组中删除自己
                    wx.cloud.callFunction({
                        name: 'deleteGroupMember',
                        data: {
                          groupId: that.data.groupId,
                          deleteAll: that.data.deleteAll
                        }
                      })
                      .then(res3 => {
                        if (that.data.deleteAll) { //如果是组长，把原来组删除了
                          workingGroupDB.doc(that.data.groupId).remove().then(res4 => {
                            that.resetLeader(that.data.groupId)
                            that.backUser('创建成功')
                          })
                        } else {
                          that.backUser('创建成功')
                        }
                      })
                  } else {
                    that.backUser('创建成功')
                  }
                }
              })
          })
      } else {
        //修改组
        workingGroupDB.doc(that.data.groupId).update({
            data: {
              groupName: that.data.groupName
            }
          })
          .then(res => {

            that.backUser('修改成功')
          })
      }
    } else {
      $Toast({
        content: '请输入组名',
        image: '../../images/icon/despise.png'
      });
    }
  },
  resetLeader(groupId) {
    let that = this
    wx.cloud.callFunction({ //获取下级
      name: 'getSubGroup',
      data: {
        type: '0',
        groupId: groupId
      }
    }).then(res => {
      if (res.result.length > 0) {
        let groupIds = []
        for (let i = 0; i < res.result.length; i++) {
          groupIds.push(res.result[i]._id)
        }
        wx.cloud.callFunction({
          name: 'resetLeader',
          data: {
            groupIds: groupIds
          }
        }).then(res2 => {
          // console.log(res2)
        })
      }
    })
  },
  backUser(title) {
    app.globalData.flushGroup = true
    app.globalData.flushView = true
    app.globalData.flushExport = true
    app.globalData.flushManage = true
    app.globalData.flushQrCode = true
    wx.switchTab({
      url: '/pages/user/user',
      success: function(res) {
        wx.showToast({
          title: title,
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  inputGroupName(e) {
    this.setData({
      groupName: e.detail.detail.value
    })
  }

})